﻿document.write([
    "<link rel='stylesheet' type='text/css' href='" + SiteConfig.FrameworkScriptUrl + "vui.1.0/plugin/webuploader/webuploader.css'>",
    "<script type='text/javascript' src='" + SiteConfig.FrameworkScriptUrl + "vui.1.0/plugin/webuploader/webuploader.js?r=2'></script>"
].join(""));
(function (_this) {

    _this.Uploader = (function (options) {
        var __this = new Object();
        __this.uploader = {};
        __this.config = {
            id: '',
            pick: '',
            auto: true,
            resize: false,
            formData: {},
            chunked: true,
            chunkSize: 5242880,
            chunkRetry: 3,
            threads: 1,
            fileNumLimit: 100,
            fileSizeLimit: 104857600,
            fileSingleSizeLimit: 104857600,
            server: SiteConfig.FrameworkServiceUrl + "Main.ashx?option=func&funcid=SystemUploader_Insert",
            swf: SiteConfig.FrameworkScriptUrl + 'vui.1.0/plugin/webuploader/Uploader.swf',
            accept: {
                title: 'files',
                extensions: 'zip,rar,7z'
            },
            fileQueued: function (file, uploader) {

                if (__this.config.pick.length == 0) {
                    $(__this.config.id + '_list').append('<div id="' + file.id + '" class="item">' +
                        '<h4 class="info">' + file.name + '</h4>' +
                        '<p class="state">等待上传...</p>' +
                        '</div>');
                }
            },
            uploadProgress: function (file, percentage, uploader) {


                if (__this.config.pick.length == 0) {
                    var $li = $('#' + file.id),
                        $percent = $li.find('.progress .progress-bar');

                    if (!$percent.length) {
                        $percent = $('<div class="progress progress-striped active">' +
                            '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                            '</div>' +
                            '</div>').appendTo($li).find('.progress-bar');
                    }
                    $li.find('p.state').text('上传中');
                    $percent.css('width', percentage * 100 + '%');
                }
            },
            uploadSuccess: function (file, response, uploader) {


                if (__this.config.pick.length == 0) {
                    $('#' + file.id).find('p.state').text('已上传');
                }
            },
            uploadError: function (file, reason, uploader) {

                if (__this.config.pick.length == 0) {
                    $('#' + file.id).find('p.state').text('上传出错');
                }
            },
            uploadComplete: function (file, uploader) {

                if (__this.config.pick.length == 0) {
                    $('#' + file.id).find('.progress').fadeOut();
                }
            },
            error: function (type, uploader) {
                console.log(type)
                switch (type) {
                    case 'Q_EXCEED_NUM_LIMIT':

                        vui.Dialog.alert({ msg: '文件数量不能超出' + __this.config.fileNumLimit + '个' });
                        break;
                    case 'Q_EXCEED_SIZE_LIMIT':

                        vui.Dialog.alert({ msg: '文件总大小不能超出' + __this.config.fileSizeLimit / 1024 / 1024 + 'MB(' + +__this.config.fileSizeLimit + '字节)' });
                        break;
                    case 'Q_TYPE_DENIED':

                        vui.Dialog.alert({ msg: '只能上传' + __this.config.accept.extensions + '格式的文件' });
                        break;
                    case 'F_EXCEED_SIZE':

                        vui.Dialog.alert({ msg: '文件大小不能超出' + __this.config.fileSingleSizeLimit / 1024 / 1024 + 'MB(' + +__this.config.fileSingleSizeLimit + '字节)' });
                        break;
                    case 'F_DUPLICATE':

                        vui.Dialog.alert({ msg: '文件不能重复上传' });
                        break;
                    default:

                        vui.Dialog.alert({ msg: '只能上传' + __this.config.accept.extensions + '格式的文件' });
                        break;
                }



            }
        };
        __this.singlefile = function (options) {
            __this.config = $.extend(true, __this.config, options);
            var _id = __this.config.id.replace('.', '').replace('#', '');
            if (__this.config.pick.length == 0) {
                $(__this.config.id).html('<div id="' + _id + '_list" class="uploader-list"></div><div class="btns"><div id="' + _id + '_pick" class="uploader-pick">选择文件</div>' +
                    (__this.config.auto ? '' : '<button id="' + _id + '_btn" class="btn btn-default">开始上传</button>') +
                    '</div>');
            }
            __this.uploader = new WebUploader.create({
                auto: __this.config.auto,

                swf: __this.config.swf,

                server: __this.config.server,


                pick: __this.config.pick.length == 0 ? __this.config.id + '_pick' : __this.config.pick,

                accept: __this.config.accept,
                resize: __this.config.resize,
                formData: __this.config.formData,
                chunked: __this.config.chunked,
                chunkSize: __this.config.chunkSize,
                chunkRetry: __this.config.chunkRetry,
                threads: __this.config.threads,
                fileNumLimit: __this.config.fileNumLimit,
                fileSizeLimit: __this.config.fileSizeLimit,
                fileSingleSizeLimit: __this.config.fileSingleSizeLimit
            });
            __this.uploader.on('fileQueued', function (file) { __this.config.fileQueued(file, __this.uploader); });
            __this.uploader.on('uploadProgress', function (file, percentage) { __this.config.uploadProgress(file, percentage, __this.uploader); });
            __this.uploader.on('uploadSuccess', function (file, response) { __this.config.uploadSuccess(file, response, __this.uploader); });
            __this.uploader.on('uploadError', function (file, reason) { __this.config.uploadError(file, reason, __this.uploader); });
            __this.uploader.on('uploadComplete', function (file) { __this.config.uploadComplete(file, __this.uploader); });
            __this.uploader.on('error', function (type) { __this.config.error(type, __this.uploader); });
            if (!__this.config.auto) {
                $(__this.config.id + '_btn').on('click', function () {
                    if ($(this).hasClass('disabled')) {
                        return false;
                    }
                    if (__this.uploader.state === 'ready') {
                        __this.uploader.upload();
                    } else if (__this.uploader.state === 'paused') {
                        __this.uploader.upload();
                    } else if (__this.uploader.state === 'uploading') {
                        __this.uploader.stop();
                    }
                });
            }
            return __this;
        };

        return __this;
    })();

    _this.WebUploader = function (options) {
        var __this = new Object();
        __this.uploader = {};
        __this.config = {
            id: '',
            pick: '',
            auto: true,
            resize: false,
            formData: {},
            chunked: true,
            chunkSize: 5242880,
            chunkRetry: 3,
            threads: 1,
            fileNumLimit: 100,
            fileSizeLimit: 104857600,
            fileSingleSizeLimit: 104857600,
            server: SiteConfig.FrameworkServiceUrl + "Main.ashx?option=func&funcid=SystemUploader_Insert",
            swf: SiteConfig.FrameworkScriptUrl + 'vui.1.0/plugin/webuploader/Uploader.swf',
            accept: {
                title: 'files',
                extensions: 'zip,rar,7z'
            },
            fileQueued: function (file, uploader) {

                if (__this.config.pick.length == 0) {
                    $(__this.config.id + '_list').append('<div id="' + file.id + '" class="item">' +
                        '<h4 class="info">' + file.name + '</h4>' +
                        '<p class="state">等待上传...</p>' +
                        '</div>');
                }
            },
            uploadProgress: function (file, percentage, uploader) {


                if (__this.config.pick.length == 0) {
                    var $li = $('#' + file.id),
                        $percent = $li.find('.progress .progress-bar');

                    if (!$percent.length) {
                        $percent = $('<div class="progress progress-striped active">' +
                            '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                            '</div>' +
                            '</div>').appendTo($li).find('.progress-bar');
                    }
                    $li.find('p.state').text('上传中');
                    $percent.css('width', percentage * 100 + '%');
                }
            },
            uploadSuccess: function (file, response, uploader) {
                if (__this.config.pick.length == 0) {
                    $('#' + file.id).find('p.state').text('已上传');
                }
            },
            uploadError: function (file, reason, uploader) {

                if (__this.config.pick.length == 0) {
                    $('#' + file.id).find('p.state').text('上传出错');
                }
            },
            uploadComplete: function (file, uploader) {

                if (__this.config.pick.length == 0) {
                    $('#' + file.id).find('.progress').fadeOut();
                }
            },
            error: function (type, uploader) {
                console.log(type)
                switch (type) {
                    case 'Q_EXCEED_NUM_LIMIT':

                        vui.Dialog.alert({ msg: '文件数量不能超出' + __this.config.fileNumLimit + '个' });
                        break;
                    case 'Q_EXCEED_SIZE_LIMIT':

                        vui.Dialog.alert({ msg: '文件总大小不能超出' + __this.config.fileSizeLimit / 1024 / 1024 + 'MB(' + +__this.config.fileSizeLimit + '字节)' });
                        break;
                    case 'Q_TYPE_DENIED':

                        vui.Dialog.alert({ msg: '只能上传' + __this.config.accept.extensions + '格式的文件' });
                        break;
                    case 'F_EXCEED_SIZE':

                        vui.Dialog.alert({ msg: '文件大小不能超出' + __this.config.fileSingleSizeLimit / 1024 / 1024 + 'MB(' + +__this.config.fileSingleSizeLimit + '字节)' });
                        break;
                    case 'F_DUPLICATE':

                        vui.Dialog.alert({ msg: '文件不能重复上传' });
                        break;
                    default:

                        vui.Dialog.alert({ msg: '只能上传' + __this.config.accept.extensions + '格式的文件' });
                        break;
                }



            }
        };


        __this.config = $.extend(true, __this.config, options);
        var _id = __this.config.id.replace('.', '').replace('#', '');
        if (__this.config.pick.length == 0) {
            $(__this.config.id).html('<div id="' + _id + '_list" class="uploader-list"></div><div class="btns"><div id="' + _id + '_pick" class="uploader-pick">选择文件</div>' +
                (__this.config.auto ? '' : '<button id="' + _id + '_btn" class="btn btn-default">开始上传</button>') +
                '</div>');
        }
        __this.uploader = new WebUploader.create({
            auto: __this.config.auto,

            swf: __this.config.swf,

            server: __this.config.server,


            pick: __this.config.pick.length == 0 ? __this.config.id + '_pick' : __this.config.pick,

            accept: __this.config.accept,
            resize: __this.config.resize,
            formData: __this.config.formData,
            chunked: __this.config.chunked,
            chunkSize: __this.config.chunkSize,
            chunkRetry: __this.config.chunkRetry,
            threads: __this.config.threads,
            fileNumLimit: __this.config.fileNumLimit,
            fileSizeLimit: __this.config.fileSizeLimit,
            fileSingleSizeLimit: __this.config.fileSingleSizeLimit
        });
        __this.uploader.on('fileQueued', function (file) { __this.config.fileQueued(file, __this.uploader); });
        __this.uploader.on('uploadProgress', function (file, percentage) { __this.config.uploadProgress(file, percentage, __this.uploader); });
        __this.uploader.on('uploadSuccess', function (file, response) { __this.config.uploadSuccess(file, response, __this.uploader); });
        __this.uploader.on('uploadError', function (file, reason) { __this.config.uploadError(file, reason, __this.uploader); });
        __this.uploader.on('uploadComplete', function (file) { __this.config.uploadComplete(file, __this.uploader); });
        __this.uploader.on('error', function (type) { __this.config.error(type, __this.uploader); });
        if (!__this.config.auto) {
            $(__this.config.id + '_btn').on('click', function () {
                if ($(this).hasClass('disabled')) {
                    return false;
                }
                if (__this.uploader.state === 'ready') {
                    __this.uploader.upload();
                } else if (__this.uploader.state === 'paused') {
                    __this.uploader.upload();
                } else if (__this.uploader.state === 'uploading') {
                    __this.uploader.stop();
                }
            });
        }
        return __this;

    };
})(vui);
